<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-5
 * Time: 下午3:48
 * To change this template use File | Settings | File Templates.
 */
class ModelCatalogTelephone extends Model {
//public function getTelephone($id) {
//    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "telephone WHERE id = '" . (int)$id . "'");
//
//    return $query->row;
//}
    public function getTotalTelephones() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "telephone where status = 'ACTIVE'");

        return $query->row['total'];
    }

    public function getTelephones($data = array()) {
        $sql = "SELECT * FROM " . DB_PREFIX . "telephone where status = 'ACTIVE'";
//        if (!empty($data['filter_name'])) {
//            $sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
//        }
//
//        if (!empty($data['filter_attribute_group_id'])) {
//            $sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
//        }

        $sort_data = array(
            'id',
            'telephone'
//            'a.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY id";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 6;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

}
